package com.chengshi.shop.controller.mobile.order;

import com.chengshi.shop.controller.common.BaseController;
import com.chengshi.shop.model.member.Member;
import com.chengshi.shop.model.order.Order;
import com.chengshi.shop.model.order.OrderInputBean;
import com.chengshi.shop.service.order.OrderPaymentService;
import com.chengshi.shop.service.order.OrderSaveService;
import com.chengshi.shop.service.order.OrderService;
import com.chengshi.shop.util.EnumUtil;
import com.chengshi.shop.util.MessageUtils;
import com.chengshi.shop.util.SessionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * 订单相关
 *
 * @author xuxinlong
 * @version 2017年07月18日
 */
@Api(value = "order", tags ="订单处理相关接口")
@RestController
@RequestMapping("/mobile/order")
public class MobileOrderController extends BaseController {
    @Resource
    private OrderService orderService;
    @Resource
    private OrderSaveService orderSaveService;
    @Resource
    private OrderPaymentService orderPaymentService;


    /**
     * 添加保存订单
     *
     * @param orderInputBean
     * @return
     */
    @ApiOperation(value = "提交订单")
    @ApiImplicitParam(name = "orderInputBean", value = "订单信息组合实体类", required = true, dataType = "OrderInputBean")
    @PostMapping(value = "/addOrderList")
    public HashMap<String, Object> addOrderList(@RequestBody OrderInputBean orderInputBean) {
        HashMap<String, Object> retMap = MessageUtils.success("订单提交成功");
        try {
            // 获得当前登陆用户
            Member nowMember = SessionUtils.getMember();
            orderInputBean.setMemberId(nowMember.getMemberId());
            if (orderInputBean.getAddressId() == null){
                return MessageUtils.error("收货地址不能为空");
            }
            retMap.putAll(orderSaveService.saveOrder(orderInputBean));
        } catch (Exception e) {
            retMap = MessageUtils.error(e.getMessage());
        }
        return retMap;
    }

    /**
     * 取消订单
     *
     * @param orderId
     * @return
     */
    @ApiOperation(value = "取消订单(只有待付款的订单才能取消订单)")
    @ApiImplicitParam(name = "orderId", value = "订单id", required = true, paramType = "query", dataType = "int")
    @PostMapping(value = "/cancelOrder")
    public HashMap<String, Object> cancelOrder(@RequestParam Integer orderId) {
        HashMap<String, Object> retMap = MessageUtils.success("取消订单成功");
        try {
            Order order = orderService.getOrderByOrderId(orderId);
            if (order.getStatus() == EnumUtil.ORDER_STATUS.待付款.getValue().byteValue()) {
                orderService.closeOrder(order, "买家", "买家取消订单，交易关闭");
                retMap.put("status", order.getStatus());
            } else {
                retMap = MessageUtils.error("订单状态已改变,取消订单失败");
            }
        } catch (Exception e) {
            retMap = MessageUtils.error("取消订单失败");
        }
        return retMap;
    }

    /**
     * 删除订单
     *
     * @param orderId
     * @return
     */
    @ApiOperation(value = "删除订单(只有交易关闭的订单才能删除)")
    @ApiImplicitParam(name = "orderId", value = "订单id", required = true, paramType = "query", dataType = "int")
    @PostMapping(value = "/delOrder")
    public HashMap<String, Object> delOrder(@RequestParam Integer orderId) {
        HashMap<String, Object> retMap = MessageUtils.success("删除成功");
        try {
            Order order = orderService.getOrderByOrderId(orderId);
            if (order.getStatus().intValue() == EnumUtil.ORDER_STATUS.交易关闭.getValue()) {
                orderSaveService.delOrder(order);
                retMap.put("status", order.getStatus());
            } else {
                return MessageUtils.error("当前订单状态无法删除");
            }
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }


    /**
     * 订单金额计算
     *
     * @param orderInputBean
     * @return
     */
    @ApiOperation(value = "订单金额计算")
    @ApiImplicitParam(name = "orderInputBean", value = "订单信息组合实体类", required = true, dataType = "OrderInputBean")
    @PostMapping(value = "/countOrder")
    public HashMap<String, Object> countOrder(@RequestBody OrderInputBean orderInputBean) {
        HashMap<String, Object> retMap = MessageUtils.success();
        try {
            // 获得当前登陆用户
            Member nowMember = SessionUtils.getMember();
            orderInputBean.setMemberId(nowMember.getMemberId());
            retMap.putAll(orderSaveService.countOrder(orderInputBean));
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

    /**
     * 用户确认收货
     *
     * @param orderId
     * @return
     */
    @ApiOperation(value = "订单确认收货")
    @ApiImplicitParam(name = "orderId", value = "订单id", required = true, paramType = "query", dataType = "int")
    @PostMapping(value = "/saveReceipt")
    public HashMap<String, Object> saveReceipt(@RequestParam Integer orderId) {
        HashMap<String, Object> retMap = MessageUtils.success();
        try {
            orderService.saveReceipt(orderId, "买家");
        } catch (Exception e) {
            return MessageUtils.error("确认收货失败");
        }
        return retMap;
    }

    /**
     * 获取订单可用支付方式
     *
     * @return
     */
    @ApiOperation(value = "获取订单可用支付方式")
    @GetMapping(value = "/getPaymentList")
    public HashMap<String, Object> getPaymentList() {
        HashMap<String, Object> retMap = MessageUtils.success();
        try {
            retMap.put("list", orderPaymentService.getCanUseOrderPayment(SessionUtils.getMember().getMemberId()));
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

    /**
     * 使用余额支付
     *
     * @param orderId
     * @return
     */
    @ApiOperation(value = "订单使用余额支付")
    @ApiImplicitParam(name = "orderId", value = "订单id", required = true, paramType = "query", dataType = "int")
    @PostMapping(value = "balancePay")
    public HashMap<String, Object> balancePay(@RequestParam Integer orderId) {
        HashMap<String, Object> retMap = MessageUtils.success("支付成功");
        try {
            Order order = orderService.getOrderByOrderId(orderId);
            if (order.getStatus().intValue() == EnumUtil.ORDER_STATUS.待付款.getValue()) {
                retMap.putAll(orderSaveService.balancePay(order));
            } else {
                retMap = MessageUtils.error("订单状态已改变，支付失败");
            }
        } catch (Exception e) {
            retMap = MessageUtils.error(e.getMessage());
        }
        return retMap;
    }
}
